home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Bitmap Libraries 2.0 / Lisp Interface / Library Folder Stuff / BitMaps.lisp next >
Lisp/Scheme  |  1996-03-10  |  14KB  |  402 lines

  1. ;;-*- Mode: Lisp; Package: (BITMAPS) -*-
  2. ;;
  3. ;; File BitMap.lisp Copyright (C) 1996 by John R. Montbriand.
  4. ;; All Rights Reserved.
  5. ;;
  6. ;;  Copyright (C) 1994, 1996 by John Montbriand.  All Rights Reserved.
  7. ;;
  8. ;;  Distribute freely in areas where the laws of copyright apply.
  9. ;;
  10. ;;  Use at your own risk.
  11. ;;
  12. ;;  Do not distribute modified copies.
  13. ;;
  14. ;;  These various BitMap libraries are for free!
  15. ;;
  16. ;;  See the file BitMap.txt for details.
  17. ;;
  18. ;; Macintosh Common Lisp Foreign Function Interfaces to the BitMap Libraries
  19.  
  20. ;; Before trying to use this file, you should put both
  21. ;; this file and the file BitMapsLib.o into the Library
  22. ;; folder inside of the MCL directory.
  23.  
  24.  
  25. (unless (find-package "BITMAPS") (defpackage "BITMAPS"))
  26.  
  27. (in-package :bitmaps)
  28.  
  29. (export '(new-bitmap kill-bitmap duplicate-bitmap rotate-bitmap-right
  30.                rotate-bitmap-left flip-bitmap-vertically flip-bitmap-horizontally
  31.                rotate-bitmap paint-bucket-bitmap lasso-bitmap trace-bitmap-edges
  32.                equal-bitmaps picture-to-bitmap bitmap-to-picture plot-bitmap
  33.                and-bitmaps or-bitmaps xor-bitmaps complement-bitmap 
  34.                test-bitmap-pixel set-bitmap-pixel clear-bitmap-pixel
  35.                toggle-bitmap-pixel string-to-bitmap with-focused-bitmap
  36.                get-bitmap-width get-bitmap-height))
  37.  
  38.  
  39. (require :ff)
  40.  
  41.  
  42. ;; BitMapLib.o contains a compiled version of the BitMap.c file
  43. ;; all set for loading into mcl
  44. (ff-load "ccl:library;BitMapsLib.o" :ffenv-name 'bits)
  45.  
  46.  
  47.  
  48. (deffcfun (new-bitmap "NewBitMap")
  49.    ((integer :word) (integer :word)) :ptr)
  50. (setf (documentation 'new-bitmap 'function)
  51.          "(new-bitmap width height) -> a bitmap
  52.     parameters: (width, height)
  53.     result: a new bitmap (null on error)
  54. new-bitmap returns a new empty bitmap with the
  55. specified width and height.")
  56.  
  57.  
  58.  
  59. (deffcfun (low-kill-bitmap "KillBitMap")
  60.    ((macptr :ptr)) :novalue)
  61. (defun kill-bitmap (badbits &rest other-bad-bits)
  62.    (progn
  63.       (low-kill-bitmap badbits)
  64.       (unless (null other-bad-bits)
  65.          (dolist (x other-bad-bits)
  66.             (low-kill-bitmap x)))))
  67. (setf (documentation 'kill-bitmap 'function)
  68.       "(kill-bitmap bitmap &rest other-bitmaps)
  69.     parameters: one or more bitmaps
  70.     result: none
  71. kill-bitmap disposes of one of more bitmaps created by
  72. new-bitmap, duplicate-bitmap, rotate-bitmap-right, rotate-bitmap-left,
  73. flip-bitmap-vertically, flip-bitmap-horizontally, rotate-bitmap,
  74. paint-bucket-bitmap, lasso-bitmap, trace-bitmap-edges,
  75. picture-to-bitmap, and-bitmaps, or-bitmaps, xor-bitmaps,
  76. complement-bitmap, or string-to-bitmap.  It's your general
  77. all purpose bitmap disposal function.")
  78.  
  79.  
  80.  
  81. (deffcfun (duplicate-bitmap "DuplicateBitMap")
  82.    ((macptr :ptr)) :ptr)
  83. (setf (documentation 'duplicate-bitmap 'function)
  84.       "(duplicate-bitmap bitmap) -> a bitmap
  85.     parameters: a bitmap
  86.     result: another bitmap
  87. duplicate-bitmap creates an exact duplicate of the bitmap
  88. argument.  The resulting bitmap will contain the same image
  89. and will have the same dimensions.")
  90.  
  91.  
  92.  
  93. (deffcfun (rotate-bitmap-right "RotateRight")
  94.    ((macptr :ptr)) :ptr)
  95. (setf (documentation 'rotate-bitmap-right 'function)
  96.       "(rotate-bitmap-right bitmap) -> a bitmap
  97.     parameters: a bitmap
  98.     result: another bitmap
  99. rotate-bitmap-right returns a new bitmap containing the
  100. same image as the parameter rotated 90 degrees to the right.")
  101.  
  102.  
  103.  
  104. (deffcfun (rotate-bitmap-left "RotateLeft")
  105.    ((macptr :ptr)) :ptr)
  106. (setf (documentation 'rotate-bitmap-left 'function)
  107.       "(rotate-bitmap-left bitmap) -> a bitmap
  108.     parameters: a bitmap
  109.     result: another bitmap
  110. rotate-bitmap-right returns a new bitmap containing the
  111. same image as the parameter rotated 90 degrees to the left")
  112.  
  113.  
  114.  
  115. (deffcfun (flip-bitmap-vertically "FlipVertical")
  116.    ((macptr :ptr)) :ptr)
  117. (setf (documentation 'flip-bitmap-vertically 'function)
  118.       "(flip-bitmap-vertically bitmap) -> a bitmap
  119.     parameters: a bitmap
  120.     result: another bitmap
  121. flip-bitmap-vertically returns a new bitmap containing the
  122. same image as the parameter flipped upside down")
  123.  
  124.  
  125.  
  126. (deffcfun (flip-bitmap-horizontally "FlipHorizontal")
  127.    ((macptr :ptr)) :ptr)
  128. (setf (documentation 'flip-bitmap-horizontally 'function)
  129.       "(flip-bitmap-horizontally bitmap) -> a bitmap
  130.     parameters: a bitmap
  131.     result: another bitmap
  132. flip-bitmap-horizontally returns a new bitmap containing the
  133. same image as the parameter flipped horizontally.")
  134.  
  135.  
  136.  
  137. (deffcfun (rotate-bitmap "iRotateBitMap")
  138.    ((macptr :ptr) (integer :word) (integer :word) (integer :word)) :ptr)
  139. (setf (documentation 'rotate-bitmap 'function)
  140.       "(rotate-bitmap bitmap h-center v-center angle) -> a bitmap
  141.     parameters: a bitmap, horizontal and vertical center of rotation, and an angle
  142.     result: another bitmap
  143. rotate-bitmap returns a new bitmap containing the image from the
  144. parameter bitmap rotated angle degrees about the specified center
  145. of rotation.")
  146.  
  147.  
  148.  
  149. (deffcfun (paint-bucket-bitmap "PaintBucketBitMap")
  150.    ((macptr :ptr) (integer :word) (integer :word)) :ptr)
  151. (setf (documentation 'paint-bucket-bitmap 'function)
  152.       "(paint-bucket-bitmap bitmap h v) -> a bitmap
  153.     parameters: a bitmap, horizontal and vertical starting point
  154.     result: another bitmap
  155. paint-bucket-bitmap returns a new bitmap containing containing
  156. a mask calculated using the SeedFill routine.")
  157.  
  158.  
  159.  
  160. (deffcfun (lasso-bitmap "LassoBitMap")
  161.    ((macptr :ptr)) :ptr)
  162. (setf (documentation 'lasso-bitmap 'function)
  163.       "(lasso-bitmap bitmap) -> a bitmap
  164.     parameters: a bitmap
  165.     result: another bitmap
  166. lasso-bitmap returns a new bitmap containing containing
  167. a mask calculated using the CalcMask routine.")
  168.  
  169.  
  170.  
  171. (deffcfun (trace-bitmap-edges "TraceBitMap")
  172.    ((macptr :ptr)) :ptr)
  173. (setf (documentation 'trace-bitmap-edges 'function)
  174.       "(trace-bitmap-edges bitmap) -> a bitmap
  175.     parameters: a bitmap
  176.     result: another bitmap
  177. trace-bitmap-edges returns a new bitmap containing containing
  178. the image from the original bitmap with its edges traced.")
  179.  
  180.  
  181.  
  182. (deffcfun (low-equal-bitmaps "EqualBitMaps")
  183.    ((macptr :ptr) (macptr :ptr)) :char)
  184.  
  185. (defun equal-bitmaps (bitmap-a bitmap-b)
  186. "(equal-bitmaps bitmap-a bitmap-b) -> T or NIL
  187.     parameters: two bitmaps
  188.     result: zero or one
  189. equal-bitmaps returns T if the two bitmaps are equal (they have
  190. the same dimensions and they contain the same image) or NIL if
  191. they are not equal."
  192.    (eql 1 (logand (char-code (low-equal-bitmaps bitmap-a bitmap-b)) #x000000FF)))
  193.  
  194.  
  195.  
  196. (deffcfun (picture-to-bitmap "PICTToBitMap")
  197.    ((macptr :ptr)) :ptr)
  198. (setf (documentation 'picture-to-bitmap 'function)
  199. "(picture-to-bitmap bitmap) -> a bitmap
  200.     parameters: a bitmap
  201.     result: a handle to a macintosh picture
  202. picture-to-bitmap returns a returns a bitmap containing
  203. a black and white representation of the image drawn by
  204. the picture parameter.")
  205.  
  206.  
  207.  
  208. (deffcfun (bitmap-to-picture "BitMapToPICT")
  209.    ((macptr :ptr)) :ptr)
  210. (setf (documentation 'bitmap-to-picture 'function)
  211. "(bitmap-to-picture bitmap) -> a picture handle
  212.     parameters: a picture handle
  213.     result: a bitmap
  214. bitmap-to-picture returns a picture handle that will
  215. draw the image stored in the bitmap.")
  216.  
  217.  
  218.  
  219. (deffcfun (plot-bitmap "PlotBitMap")
  220.    ((macptr :ptr) (integer :word) (integer :word) (integer :word)) :novalue)
  221. (setf (documentation 'plot-bitmap 'function)
  222. "(plot-bitmap bitmap hpos vpos mode)
  223.     parameters: a bitmap, the horizontal and vertical position, and the drawing mode
  224.     result: a bitmap
  225. plot-bitmap draws the bitmap parameter to the current port at the
  226. indicated position using the specified drawing mode.  mode can be one
  227. of:  #$srcCopy #$srcOr #$srcXor #$srcBic #$notSrcCopy
  228.     #$notSrcOr #$notSrcXor #$notSrcBic #$patCopy #$patOr #$patXor
  229.     #$patBic #$notPatCopy #$notPatOr #$notPatXor #$notPatBic")
  230.  
  231.  
  232.  
  233. (deffcfun (and-bitmaps "BitMapAND")
  234.    ((macptr :ptr) (macptr :ptr)) :ptr)
  235. (setf (documentation 'and-bitmaps 'function)
  236. "(and-bitmaps bitmap-a bitmap-b) -> a bitmap
  237.     parameters: two bitmaps with identical dimensions
  238.     result: a bitmap
  239. and-bitmaps returns a new bitmap with the same dimensions
  240. as the two parameter bitmaps.  the raster data in the resulting
  241. bitmap will be the result of logically and-ing together the raster
  242. data from the two parameter bitmaps.")
  243.  
  244.  
  245.  
  246. (deffcfun (or-bitmaps "BitMapOR")
  247.    ((macptr :ptr) (macptr :ptr)) :ptr)
  248. (setf (documentation 'or-bitmaps 'function)
  249. "(or-bitmaps bitmap-a bitmap-b) -> a bitmap
  250.     parameters: two bitmaps with identical dimensions
  251.     result: a bitmap
  252. or-bitmaps returns a new bitmap with the same dimensions
  253. as the two parameter bitmaps.  the raster data in the resulting
  254. bitmap will be the result of logically or-ing together the raster
  255. data from the two parameter bitmaps.")
  256.  
  257.  
  258.  
  259. (deffcfun (xor-bitmaps "BitMapXOR")
  260.    ((macptr :ptr) (macptr :ptr)) :ptr)
  261. (setf (documentation 'xor-bitmaps 'function)
  262. "(xor-bitmaps bitmap-a bitmap-b) -> a bitmap
  263.     parameters: two bitmaps with identical dimensions
  264.     result: a bitmap
  265. xor-bitmaps returns a new bitmap with the same dimensions
  266. as the two parameter bitmaps.  the raster data in the resulting
  267. bitmap will be the result of logically xor-ing together the raster
  268. data from the two parameter bitmaps.")
  269.  
  270.  
  271.  
  272. (deffcfun (complement-bitmap "BitMapNOT")
  273.    ((macptr :ptr)) :ptr)
  274. (setf (documentation 'complement-bitmap 'function)
  275. "(complement-bitmap bitmap) -> a bitmap
  276.     parameters: a bitmap
  277.     result: another bitmap
  278. complement-bitmap returns a new bitmap with the same dimensions
  279. as the parameter bitmap.  the raster data in the resulting
  280. bitmap will be the result of logically complementing the
  281. raster data from the parameter bitmap.")
  282.  
  283.  
  284.  
  285. (deffcfun (low-test-bitmap-pixel "BitMapTest")
  286.    ((macptr :ptr) (integer :word) (integer :word)) :char)
  287.  
  288. (defun test-bitmap-pixel (bits x y)
  289. "(test-bitmap-pixel bitmap x y) -> T or NIL
  290.     parameters: a bitmap and a horizontal and vertical position
  291.     result: T or NIL
  292. test-bitmap-pixel returns T if the specified pixel
  293. at location (x,y) is equal to one.  Otherwise the function
  294. returns NIL"
  295.    (eql 1 (logand (char-code (low-test-bitmap-pixel bits x y)) #x000000FF)))
  296.  
  297.  
  298.  
  299. (deffcfun (set-bitmap-pixel "BitMapSet")
  300.    ((macptr :ptr) (integer :word) (integer :word)) :novalue)
  301. (setf (documentation 'set-bitmap-pixel 'function)
  302. "(set-bitmap-pixel bitmap hpos vpos)
  303.     parameters: a bitmap and a horizontal and vertical location
  304.     result: another bitmap
  305. set-bitmap-pixel sets the indicated pixel in the bitmap's raster
  306. image to the value one. ")
  307.  
  308.  
  309.  
  310. (deffcfun (clear-bitmap-pixel "BitMapClear")
  311.    ((macptr :ptr) (integer :word) (integer :word)) :novalue)
  312. (setf (documentation 'clear-bitmap-pixel 'function)
  313. "(clear-bitmap-pixel bitmap hpos vpos)
  314.     parameters: a bitmap and a horizontal and vertical location
  315.     result: another bitmap
  316. set-bitmap-pixel sets the indicated pixel in the bitmap's raster
  317. image to the value zero. ")
  318.  
  319.  
  320. (deffcfun (low-toggle-bitmap-pixel "BitMapToggle")
  321.    ((macptr :ptr) (integer :word) (integer :word)) :char)
  322.  
  323. (defun toggle-bitmap-pixel (bits x y)
  324. "(toggle-bitmap-pixel bitmap x y) -> T or NIL
  325.     parameters: a bitmap and a horizontal and vertical position
  326.     result: T or NIL
  327. toggle-bitmap-pixel toggles a pixel in in the bitmap at the indicated
  328. position and returns T or false indicating the state of the pixel after
  329. the toggle. "
  330.    (eql 1 (logand (char-code (low-toggle-bitmap-pixel bits x y)) #x000000FF)))
  331.  
  332.  
  333.  
  334. (deffcfun (low-string-to-bitmap "StringToBitMap")
  335.    ((integer :word) (integer :word) (integer :word) (string :pstring)) :ptr)
  336.  
  337. (defun string-to-bitmap (the-string &optional the-font-spec)
  338. "(string-to-bitmap the-string &optional the-font-spec) -> a bitmap
  339.     parameters: a string and an optional font spec
  340.     result: a bitmap
  341. string-to-bitmap returns a bitmap sized appropriately to contain
  342. the string parameter.  if the font spec is omitted, the system font
  343. is used. "
  344.    (if (null the-font-spec)
  345.       (low-string-to-bitmap 0 12 0 the-string)    ; use 12 point system font 
  346.       (multiple-value-bind (ff ms) (font-codes the-font-spec)
  347.           (low-string-to-bitmap
  348.             (ash (logand ff #xFFFF0000) -16)   ; the font number
  349.             (logand ms #x0000FFFF)             ; the font size
  350.             (ash (logand ff #x0000FF00) -8)    ; the text face
  351.             the-string))))
  352.  
  353.  
  354.  
  355. (defmacro with-focused-bitmap ((the-bitmap) &body body)
  356. "(with-focused-bitmap bitmap {form}*) -> bitmap
  357.     parameters: a bitmap and some forms
  358.     result: the bitmap
  359. sets up the current drawing enviroment so that all drawing commands
  360. go into the bitmap and executes the forms.  Before exit, the original
  361. grafport is restored. "
  362.    `(without-interrupts
  363.       (rlet ((myport :GrafPort) (current-port :GrafPtr))
  364.          (require-trap #_GetPort current-port)
  365.          (require-trap #_OpenPort myport)
  366.          (require-trap #_SetPortBits ,the-bitmap)
  367.          (require-trap #_PortSize
  368.                                (rref ,the-bitmap bitmap.bounds.right)
  369.                                (rref ,the-bitmap bitmap.bounds.bottom))
  370.          (unwind-protect
  371.             (progn ,@body)
  372.             (progn
  373.                (require-trap #_SetPort (%get-ptr current-port))
  374.                (require-trap #_ClosePort myport)))
  375.          ,the-bitmap)))
  376.  
  377.  
  378.  
  379. (defun get-bitmap-width (the-bitmap)
  380. "(get-bitmap-width bitmap) -> the width
  381.     parameters: a bitmap
  382.     result: a number
  383. get-bitmap-width returns a number representing the total
  384. width of the bitmap. "
  385.    (- (rref the-bitmap bitmap.bounds.right) (rref the-bitmap bitmap.bounds.left)))
  386.  
  387.  
  388.  
  389. (defun get-bitmap-height (the-bitmap)
  390. "(get-bitmap-height bitmap) -> the height
  391.     parameters: a bitmap
  392.     result: a number
  393. get-bitmap-height returns a number representing the total
  394. height of the bitmap. "
  395.    (- (rref the-bitmap bitmap.bounds.bottom) (rref the-bitmap bitmap.bounds.top)))
  396.  
  397.  
  398. ;; end of file BitMaps.lisp
  399.  
  400.  
  401.  
  402.